<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $username
 * @property string $full_name
 * @property string $password
 * @property string $email
 * @property integer $public
 * @property integer $active
 * @property string $avatar
 * @property string $location
 * @property string $website
 * @property string $bio
 * @property string $created_date
 *
 * The followings are the available model relations:
 * @property Favorites[] $favorites
 * @property Follows[] $follows
 * @property Follows[] $follows1
 * @property Gcm[] $gcms
 * @property Mentions[] $mentions
 * @property Timeline[] $timelines
 * @property Tweets[] $tweets
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, full_name, password, email, created_date', 'required'),
			array('public, active', 'numerical', 'integerOnly'=>true),
                        array('username, email', 'unique'),
                        array('email', 'email'),
			array('username, password', 'length', 'max'=>50),
			array('full_name', 'length', 'max'=>250),
			array('email', 'length', 'max'=>25),
			array('avatar, bio', 'length', 'max'=>255),
			array('location, website', 'length', 'max'=>150),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, full_name, password, email, public, active, avatar, location, website, bio, created_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'favorites' => array(self::HAS_MANY, 'Favorites', 'user_id'),
			'following' => array(self::HAS_MANY, 'Follows', 'follow_id'),
			'followers' => array(self::HAS_MANY, 'Follows', 'user_id'),
			'mentions' => array(self::HAS_MANY, 'Mentions', 'user_id'),
			'timelines' => array(self::HAS_MANY, 'Timeline', 'user_id'),
			'tweets' => array(self::HAS_MANY, 'Tweets', 'user_id'),
                        'gcms'   => array(self::HAS_MANY, 'Gcm', 'user_id'),
                        'count_following' => array(self::STAT, 'Follows', 'follow_id'),
                        'count_followers' => array(self::STAT, 'Follows', 'user_id'),
                        'count_tweets'     => array(self::STAT, 'Tweets', 'id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'full_name' => 'Full Name',
			'password' => 'Password',
			'email' => 'Email',
			'public' => 'Public',
			'active' => 'Active',
			'avatar' => 'Avatar',
			'location' => 'Location',
			'website' => 'Website',
			'bio' => 'Bio',
			'created_date' => 'Created Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('public',$this->public);
		$criteria->compare('active',$this->active);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('bio',$this->bio,true);
		$criteria->compare('created_date',$this->created_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}